package com.itlong.cloud.app.service.impl;

import com.itlong.cloud.POJO.VO.app.AppUserCacheVO;
import com.itlong.cloud.PlatformConstants;
import com.itlong.cloud.app.service.IAppRegisterService;
import com.itlong.cloud.app.service.IAppUserInfoCacheService;
import com.itlong.cloud.constants.RedisConstant;
import com.itlong.cloud.service.RedisService;
import com.itlong.cloud.thrown.DataAccessException;
import com.itlong.cloud.utils.date.DateUtil;
import com.itlong.cloud.utils.encrypt.UserTokenUtil;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.data.redis.RedisConnectionFailureException;
import org.springframework.stereotype.Service;

/**
 * <desc>
 *      APP用户信息业务处理接口实现类
 * </desc>
 *
 * @createDate 2017/10/19
 */
@Service
@RefreshScope
public class AppUserInfoCacheServiceImpl implements IAppUserInfoCacheService {
    private static final Logger LOG = LoggerFactory.getLogger(AppUserInfoCacheServiceImpl.class);
    @Autowired
    RedisService<AppUserCacheVO> redisService;

    @Autowired
    IAppRegisterService iAppRegisterService;

    @Value("${" + PlatformConstants.APP_USER_TOKEN_KEY + "}")
    public String APP_USER_TOKEN_KEY;

    /**
     * <desc>
     *      从缓存获取用户信息
     * </desc>
     *
     * @param token
     * @return
     * @author Jiaqi.X
     * @createDate 2017/10/19
     */
    @Override
    public AppUserCacheVO retrieve(String token) {
        try {
            if (StringUtils.isBlank(token)) {
                return null;
            }
            //解密拿到userId
            String userId = UserTokenUtil.newAPPTokenDecrypt(token, APP_USER_TOKEN_KEY);
            if (userId == null) {
                return null;
            }

            AppUserCacheVO userInfo = redisService.getObject(RedisConstant.APP_LOGIN+userId, AppUserCacheVO.class);
            if (userInfo != null && token.equals(userInfo.getToken())) {
                Integer userStatus = iAppRegisterService.getAppUserStatusByUserId(userId);
                if (userStatus == null){
                    userInfo.setStatus(1);
                }else {
                    userInfo.setStatus(userStatus);
                }
                return userInfo;
            } else {
                userInfo = iAppRegisterService.resetUserLogin(userId,token);
                //LOG.info("【新版APP-Token拦截成功】--userId:" + userId + "--" + DateUtil.getCurrentTime() + "--缓存不存在--备用查询用户");
                if (userInfo!=null && userInfo.getStatus() == null){
                    userInfo.setStatus(1);
                }
                if (userInfo == null){
                    LOG.info("【新版APP-Token拦截成功】--userId:" +userId+"--token:"+token+"--"+ DateUtil.getCurrentTime()+"--缓存不存在--备用查询用户不存在");
                }
                return userInfo;
            }
        }catch (RedisConnectionFailureException e){
            LOG.error("【新版APP-Token拦截成功】--"+ DateUtil.getCurrentTime()+"--APP端retrieve【从缓存中获取用户信息】redis连接失败");
            throw new DataAccessException("APP端retrieve【从缓存中获取用户信息】redis连接失败") ;
        }
    }
}
